package com.tanhua.server.controller;

import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserSerivce userSerivce;

    /**
     * 读取用户的通用设置
     * 请求连接：
     *  GET  /settings
     */
    @GetMapping("/settings")
    public ResponseEntity querySettings() {
        return settingsService.querySettings();
    }

    /**
     * 设置陌生人问题
     *  请求连接
     *      POST  /questions
     *      参数： body ：content
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestions(@RequestBody Map map) {
        //1、获取输入内容
        String content = (String) map.get("content");
        //2、调用service保存或者更新
        return settingsService.saveQuestions(content);
    }


    /**
     * 通知设置
     *   POST /notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateNotification(@RequestBody Map map) {
        //1、获取输入内容
        boolean like = (Boolean) map.get("likeNotification");
        boolean pinglun = (Boolean) map.get("pinglunNotification");
        boolean gonggao = (Boolean) map.get("gonggaoNotification");
        //2、调用service保存或者更新
        return settingsService.updateNotification(like,pinglun,gonggao);
    }

    /**
     * 分页查询黑名单列表
     *  请求连接：
     *      GET /blacklist
     *  请求参数：
     *      Query参数：
     *          page：当前页
     *          pagesize：每页查询条数
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10")  int pagesize) {
        return settingsService.queryBlacklist(page,pagesize);
    }

    /**
     * 移除黑名单
     *  请求连接：
     *      DELETE  /blacklist/{uid}
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity delBlacklist(@PathVariable("uid") long deleteUserId) {
        return settingsService.delBlacklist(deleteUserId);
    }

    /**
     * 修改手机号：1 发送手机验证码
     *  请求连接：POST /phone/sendVerificationCode
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String mobile = UserHolder.getUser().getMobile();
        return userSerivce.sendMsg(mobile);
    }

    /**
     * 修改手机号：2 检验验证码
     *  请求连接： POST /phone/checkVerificationCode
     *  参数：body类型，verificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = (String) map.get("verificationCode"); //验证码
        return userSerivce.checkVerificationCode(code);
    }

    /**
     * 修改手机号码：3 更新手机号码
     *    请求连接：POST /phone
     *    参数：body类型  phone
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String mobile = (String) map.get("phone");
        return userSerivce.updateUserMobile(mobile);
    }
}
